//
//  GWSettingCell.m
//  CROSBY
//
//  Created by 王华磊 on 2017/10/3.
//  Copyright © 2017年 Haley. All rights reserved.
//

#import "GWSettingCell.h"

@interface GWSettingCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation GWSettingCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    _switchView.hidden = YES;
    _arrowImageView.hidden = YES;
    _contentLabel.hidden = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"settingCell";
    GWSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"GWSettingCell" owner:self options:nil] lastObject];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

- (IBAction)switchChange:(UISwitch *)sender {
    if ([_delegate respondsToSelector:@selector(switchChange:onCell:)]) {
        [_delegate switchChange:sender onCell:self];
    }
}

/**
  如果含switchView，则文本最大宽度为 kWidth - (40 + 8 + 8) - (50 + 8 + 5)
  如果不含switchView，则文本最大宽度为 kWidth - (40 + 8 + 8) - (30 + 8 + 5)

 @param dict 设置字典
 */
- (void)setDict:(NSDictionary *)dict
{
    _dict = dict;
    
    CGFloat maxContant = kWidth - (40 + 8 + 8) - (50 + 8 + 5);
    
    NSString *imageName = dict[@"icon"];
    _iconImageView.image = [UIImage imageNamed:imageName];
 
    NSString *title = showText(dict[@"title"]);
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]};
    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(maxContant, 36) options:NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    _titleLabel.text = title;
    
    NSString *content = dict[@"content"];
    attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]};
    CGSize contentSize = [content boundingRectWithSize:CGSizeMake(maxContant, 36) options:NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    
    AccessViewType type = [dict[@"accessType"] integerValue];
    _switchView.hidden = YES;
    _arrowImageView.hidden = YES;
    _contentLabel.hidden = YES;
    // 默认120
    self.titleLabelWidthContant.constant = maxContant;
    self.contentLabelWidthContant.constant = 93;
    if (type == AccessViewTypeSwitch) {
        _switchView.hidden = NO;
    } else if (type == AccessViewTypeCheck) {
        _arrowImageView.hidden = NO;
        _arrowImageView.image = [UIImage imageNamed:@"setting_ico_ok"];
    } else if (type == AccessViewTypeLabel) {
        _arrowImageView.hidden = NO;
        _arrowImageView.image = [UIImage imageNamed:@"setting_into"];
        if (content.length == 0) {
            return;
        }
        // title 和 content 都存在
        _contentLabel.hidden = NO;
        _contentLabel.text = content;
        
        if (contentSize.width < maxContant * 0.5) {
            self.contentLabelWidthContant.constant = contentSize.width + 10;
            self.titleLabelWidthContant.constant = maxContant - contentSize.width - 8;
            return;
        }
        
        if (titleSize.width < maxContant * 0.5) {
            self.titleLabelWidthContant.constant = titleSize.width + 10;
            self.contentLabelWidthContant.constant = maxContant - titleSize.width - 8;
            return;
        }

        self.titleLabelWidthContant.constant = maxContant * 0.5;
        self.contentLabelWidthContant.constant = maxContant * 0.5;
    }
}

@end
